<?php

/**
 * UeditorUpService.class.php 富文本编辑器上传处理
 * Created Time 2016年3月24日 下午6:12:42
 *
 * @author     Xiaodc
 * @copyright  Copyright (c) 2005-2015 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
namespace Component\Service;

class UeditorUpService {

    public function action_crawler($CONFIG)
    {

        /* 上传配置 */
        $config = array (
                "pathFormat" => __ROOT__.$CONFIG ['catcherPathFormat'], 
                "maxSize" => $CONFIG ['catcherMaxSize'], 
                "allowFiles" => $CONFIG ['catcherAllowFiles'], 
                "oriName" => "remote.png" 
        );
        $fieldName = $CONFIG ['catcherFieldName'];
        
        /* 抓取远程图片 */
        $list = array ();
        if (isset ($_POST [$fieldName]))
        {
            $source = $_POST [$fieldName];
        }
        else
        {
            $source = $_GET [$fieldName];
        }
        foreach ($source as $imgUrl)
        {
            $item = new \Component\Common\UUploader($imgUrl, $config, "remote");
            $info = $item->getFileInfo ();
            array_push ($list, array (
                    "state" => $info ["state"], 
                    "url" => $info ["url"], 
                    "size" => $info ["size"], 
                    "title" => htmlspecialchars ($info ["title"]), 
                    "original" => htmlspecialchars ($info ["original"]), 
                    "source" => htmlspecialchars ($imgUrl) 
            ));
        }
        
        /* 返回抓取数据 */
        return json_encode (array (
                'state' => count ($list) ? 'SUCCESS' : 'ERROR', 
                'list' => $list 
        ));
    }

    public function action_list($CONFIG)
    {

    }

    public function action_upload($CONFIG)
    {
        /* 上传配置 */
        $base64 = "upload";
        switch (htmlspecialchars($_GET['action'])) {
            case 'uploadimage':
                $config = array(
                "pathFormat" => $CONFIG['imagePathFormat'],
                "maxSize" => $CONFIG['imageMaxSize'],
                "allowFiles" => $CONFIG['imageAllowFiles']
                );
                $fieldName = $CONFIG['imageFieldName'];
                break;
            case 'uploadscrawl':
                $config = array(
                "pathFormat" => $CONFIG['scrawlPathFormat'],
                "maxSize" => $CONFIG['scrawlMaxSize'],
                "allowFiles" => $CONFIG['scrawlAllowFiles'],
                "oriName" => "scrawl.png"
                        );
                        $fieldName = $CONFIG['scrawlFieldName'];
                        $base64 = "base64";
                        break;
            case 'uploadvideo':
                $config = array(
                "pathFormat" => $CONFIG['videoPathFormat'],
                "maxSize" => $CONFIG['videoMaxSize'],
                "allowFiles" => $CONFIG['videoAllowFiles']
                );
                $fieldName = $CONFIG['videoFieldName'];
                break;
            case 'uploadfile':
            default:
                $config = array(
                "pathFormat" => $CONFIG['filePathFormat'],
                "maxSize" => $CONFIG['fileMaxSize'],
                "allowFiles" => $CONFIG['fileAllowFiles']
                );
                $fieldName = $CONFIG['fileFieldName'];
                break;
        }
        
        /* 生成上传实例对象并完成上传 */
        $config['pathFormat'] = __ROOT__.$config['pathFormat'];
        $up = new \Component\Common\UUploader($fieldName, $config, $base64);
        
        /**
         * 得到上传文件所对应的各个参数,数组结构
         * array(
         *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
         *     "url" => "",            //返回的地址
         *     "title" => "",          //新文件名
         *     "original" => "",       //原始文件名
         *     "type" => ""            //文件类型
         *     "size" => "",           //文件大小
         * )
        */
        $attInfo = $up->getFileInfo();
       // var_dump(file_exists($attInfo['url']),$attInfo);
        if($attInfo['url'])
        {
            $fileinfo['atname'] = $attInfo['title'];
            $fileinfo['atmine'] = rmime_content_type($attInfo['title']);
            $fileinfo ['atext'] = substr($attInfo['title'], strrpos($attInfo['title'], '.')+1);
            if(in_array($fileinfo['atext'], C('imgexts')))
            {
                $fileinfo ['attype'] = 1;
            }
            $fileinfo ['created'] = time ();
        
            // 求出文件的保存路径
            $tmpStr = strchr($attInfo['url'], '/ueditor/');
            $tmpStr = ltrim(substr($tmpStr, 0,strripos($tmpStr, '/')+1), '/');
            $fileinfo ['atpath'] = $tmpStr;
            $attModel = D ('Attachment');
            $rs = $attModel->data ($fileinfo)->add();
            if (! $rs)
            {
                $attInfo['url'] = '';
            }
            //var_dump(111);
            $attInfo['url'] = set_attr_url($rs);
        }
        
        /* 返回数据 */
        return json_encode($attInfo);
    }

}
